package server

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"specifica_video_manage/client"
	"specifica_video_manage/model"
)

type JobServer struct{}

func (j JobServer) GetJob(c *gin.Context) {
	freshJob := client.MysqlTaskClient{}.GetUnStartJob()
	if freshJob != nil {
		// get a fresh job
		c.JSON(200, model.Resp{}.Ok(freshJob))
		return
	} else {
		c.JSON(200, model.Resp{}.Empty("not find any task"))
	}
}

func (j JobServer) UpdateState(c *gin.Context) {
	updateUrlState := model.UpdateUrlState{}
	err := c.BindJSON(&updateUrlState)
	if err != nil {
		fmt.Println("bind task catch error =", err.Error())
	}
	fmt.Println("updateUrlState:", updateUrlState)
	result := client.MysqlTaskClient{}.UpdateJobState(&updateUrlState)
	if result {
		c.JSON(200, model.Resp{}.Ok("update state success"))
		return
	} else {
		c.JSON(200, model.Resp{}.Error("update state failed"))
	}
}
